<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Orderdetail_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 根据订单ID得到订单详情
	 */
	public function find_by_order($orderid){
		$this->db->select('t_orderdetail.id,t_orderdetail.productid,t_product.name,t_orderdetail.amount,t_orderdetail.price,t_product.unit,t_product.description,t_orderdetail.date');
		$this->db->from('t_orderdetail');
		$this->db->where('t_orderdetail.orderid',$orderid);
		$this->db->join('t_product', 't_orderdetail.productid = t_product.id','left');
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 保存订单详情信息
	 */
	public function create($orderdetail){
		$this->db->insert('t_orderdetail', $orderdetail);
		return $this->db->insert_id();
	}
	/*
	 * 更新订单详情信息
	 */
	public function update($id,$orderdetail){
		$this->db->where('id', $id);
		$this->db->update('t_orderdetail', $orderdetail);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除订单详情
	 */
	public function delete_by_order($orderid){
		$this->db->where('orderid', $orderid);
		$this->db->delete('t_orderdetail');
		return $this->db->affected_rows()>0;
	}
	/*
	 *根据详情ID 删除订单详情
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_orderdetail');
		return $this->db->affected_rows()>0;
	}
	/*
	 * 根据订单ID，日期和类型返回数量(用于生成默认餐)
	 */
	public function count_by_order_date_type($orderid,$date,$type){
		$this->db->where('orderid',$orderid);
		$this->db->where('date',$date);
		$this->db->where('type',$type);
		$num = $this->db->count_all_results('t_orderdetail');
		return $num;
	}
}